package com.financing.wap.service.impl;


import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.financing.wap.common.CommonConstantUtil.CommonStatus;
import com.financing.wap.dao.IDstUserWithdrawLogDao;
import com.financing.wap.dto.DstMemberInfoDTO;
import com.financing.wap.dto.DstUserWithdrawLogDTO;
import com.financing.wap.service.ICommonDstService;
import com.financing.wap.service.IDstMemberInfoService;
import com.financing.wap.service.IDstUserWithdrawLogService;
import com.financing.wap.service.weixin.service.WeixinPayService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.DateUtil;
import com.financing.wap.util.PropertiesWXUtils;
import com.financing.wap.util.RandomUtils;
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPaySendRedpackRequest;
import com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult;
/**
 * dst_user_withdraw_log  
 * 提现积分兑换记录 业务访问层实现类
 * @author huqiyun
 *
 */
 
@Service("dstUserWithdrawLogService")
@Transactional(rollbackFor=Exception.class)
public class DstUserWithdrawLogServiceImpl implements IDstUserWithdrawLogService{

	public Logger logger = Logger.getLogger(this.getClass()); 
	
	@Autowired
	private IDstUserWithdrawLogDao dstUserWithdrawLogDao;
	
	@Autowired
	private IDstMemberInfoService dstMemberInfoService;

	@Autowired
	private ICommonDstService commonDstService;

	@Autowired
	WeixinPayService payService;
	
	@Override
	public int insert(DstUserWithdrawLogDTO dstUserWithdrawLog) throws SQLException {
		DTOdateSetUtil.setCreateInfo(dstUserWithdrawLog);
		DTOdateSetUtil.setUpdateInfo(dstUserWithdrawLog);
		dstUserWithdrawLog.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
		return dstUserWithdrawLogDao.insert(dstUserWithdrawLog);
	}

	@Override
	public void update(DstUserWithdrawLogDTO dstUserWithdrawLog) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(dstUserWithdrawLog);
		dstUserWithdrawLogDao.update(dstUserWithdrawLog);
	}

	@Override
	public void updateForce(DstUserWithdrawLogDTO dstUserWithdrawLog) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(dstUserWithdrawLog);
		dstUserWithdrawLogDao.updateForce(dstUserWithdrawLog);
	}

	@Override
	public void delete(Integer id) throws SQLException {
		dstUserWithdrawLogDao.delete(id);
	}
	
	@Override
	public List<DstUserWithdrawLogDTO> queryList(DstUserWithdrawLogDTO dstUserWithdrawLog) throws SQLException {
		return dstUserWithdrawLogDao.queryList(dstUserWithdrawLog);
	}

	@Override
	public int queryListCount(DstUserWithdrawLogDTO dstUserWithdrawLog) throws SQLException {
		return dstUserWithdrawLogDao.queryListCount(dstUserWithdrawLog);
	}

	@Override
	public DstUserWithdrawLogDTO loadById(Integer id) throws SQLException {
		return dstUserWithdrawLogDao.loadById(id);
	}
 	@Override
	public DstUserWithdrawLogDTO queryListGetFirst(DstUserWithdrawLogDTO dstUserWithdrawLog)
			throws SQLException {
		 List<DstUserWithdrawLogDTO> queryList = queryList(dstUserWithdrawLog);
		 if(queryList==null||queryList.size()==0){
			 return new DstUserWithdrawLogDTO();
		 }
		 return queryList.get(0);
	}

	@Override
	public boolean userWithdraw(String userOpenId, BigDecimal amount)
			throws  Exception {
		String mchBillNo = PropertiesWXUtils.getValue("pay.mchId") + DateUtil.getDate("yyyyMMdd")+RandomUtils.getRandomUtils(10);
		//插入提现记录
		DstUserWithdrawLogDTO dstUserWithdrawLog=new DstUserWithdrawLogDTO();
		dstUserWithdrawLog.setUserOpenid(userOpenId);
		dstUserWithdrawLog.setUserWithdrawMoney(amount);
		dstUserWithdrawLog.setWithdrawStatus(CommonStatus.WITHDRAW_STATUS_2);
		dstUserWithdrawLog.setWithdrawType(CommonStatus.WITHDRAW_TYPE_1);
		dstUserWithdrawLog.setWithdrawWxOrderno(mchBillNo);
		dstUserWithdrawLog.setWithdrawDate(new Date());
		insert(dstUserWithdrawLog);
		//扣除用户金额
		DstMemberInfoDTO member=new DstMemberInfoDTO();
		member.setUserOpenid(userOpenId);
		member.setMemberMoney(amount.multiply(new BigDecimal(-1)));
		member.setMemberWithdraw(amount);
		commonDstService.updateMemberInfo(member);
		
		//发送红包
		WxPaySendRedpackRequest res=new WxPaySendRedpackRequest();
		res.setReOpenid(userOpenId);
		res.setTotalAmount(BaseWxPayRequest.yuanToFen(amount.toString()));
		res.setWishing("提现红包");
		res.setActName("分享返现");
		res.setSendName("麒麟展业");
		res.setSceneId("PRODUCT_1");
		res.setMchBillNo(mchBillNo);
		res.setTotalNum(1);
		res.setClientIp("117.186.27.86");
		res.setRemark("测试备注");
		WxPaySendRedpackResult sendRedpack = payService.sendRedpack(res);
		logger.info(sendRedpack);
//		String mchBillno = sendRedpack.getMchBillno();
//		WxPayRedpackQueryResult queryRedpack = payService.queryRedpack(mchBillno);
//		logger.info("queryRedpack:"+queryRedpack);
		if(!"SUCCESS".equals(sendRedpack.getResultCode())||!"SUCCESS".equals(sendRedpack.getReturnCode())){
			throw new Exception("发送红包失败!!");
		}
		return true;
	}
}
